<?php 
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Userapi extends CI_Controller {
	/**
	 * 判断递交方法
	 *
	 * @param	IS_POST	//新增
	 * @param	IS_GET	//查询
	 * @param	IS_PUT  //修改
	 * @param	IS_DEL  //删除
	 *
	 * @param	$this->load->model('Model_m');  // 实例 增删改查 的模型
     * @param	$user_IP = $this->Model_m->select('app_auth',$ip_where,'id desc','1,1');  1//app_auth 数据表  2//$ip_where where数组 3//正序倒序  4//limit
	 * 	
     *
	 */

	public function __construct(){
		parent::__construct();
		header('Content-Type: application/json; charset=utf-8');
	}
	
	
    public function user_api(){
    	$this->load->model('Model_m');// 实例 增删改查 的模型
    	
    	$server_ip = $_SERVER["REMOTE_ADDR"]; //获取到的IP地址 $output = isset($value) ? $value : 'No value set.';
    	$appid = isset($_REQUEST['appid'])?$_REQUEST['appid']:'';
    	
    	$ip_where = array('appid'=>$appid,'ip'=>$server_ip); //where
    	//判断IP地址是否合法  这里用户只跟指定IP交互用户数据
    	$user_IP = $this->Model_m->select('app_auth',$ip_where,'id desc','1,1');
    		
    	if($user_IP){
			if($server_ip != $user_IP[0]['ip']){
	    		$data['error'] = $this->Model_m->data_error('4003');
	    		echo json_encode($data,JSON_UNESCAPED_UNICODE);
		    	exit;
			}	

	    	if($appid != $user_IP[0]['appid']){
	    		$data['error'] = $this->Model_m->data_error('4003');
		    	echo json_encode($data,JSON_UNESCAPED_UNICODE);
		    	exit;
			}	
    	}else{
    		$data['error'] = $this->Model_m->data_error('4001');
	    	echo json_encode($data,JSON_UNESCAPED_UNICODE);
	    	exit;
    	}
    	
    	//查询用户**
		if(IS_GET){
			//如果是GET提交
			$user_id = isset($_REQUEST['user_id'])?$_REQUEST['user_id']:'';
			$user_where = array('user_id'=>$user_id); //where
			$user = $this->Model_m->select('users',$user_where);
			
			if(empty($user_id)){
				$data['error'] = $this->Model_m->data_error('4006');
		    	echo json_encode($data,JSON_UNESCAPED_UNICODE);
		    	exit;
			}else{
				
		        $user_where = array('user_id'=>$user_id); //where
		        $user_weixin_where = array('user_id'=>$user_id); //where
		        $data['success'] = $this->Model_m->data_success('4000');
		    	$data['user'] = $this->Model_m->select('users',$user_where);
		    	$data['weixin'] = $this->Model_m->select('users_weixin',$user_weixin_where);
		    	
		    	echo json_encode($data,JSON_UNESCAPED_UNICODE);
			}
			exit;
		}
		
		//添加用户**
		if(IS_POST){
			//如果是POST提交
			$username = isset($_REQUEST['username'])?$_REQUEST['username']:'';
			$user_where = array('username'=>$username); //where
			//查询有没有这个用户
			$user_old = $this->Model_m->select('users',$user_where);

			if($user_old){
				$user_old_where = array('user_id'=>$user_old[0]['user_id']); //where
		        $user_old_weixin_where = array('user_id'=>$user_old[0]['user_id']); //where
		        $data['error'] = $this->Model_m->data_error('5000');
		    	$data['user'] = $this->Model_m->select('users',$user_old_where,'user_id asc');
		    	$data['weixin'] = $this->Model_m->select('users_weixin',$user_old_weixin_where,'id asc');
		    	
		    	echo json_encode($data,JSON_UNESCAPED_UNICODE);
			}else{
				$user_last_where = ''; //where
				$user_last = $this->Model_m->select('users',$user_last_where,'user_id desc','1,1');
				$data['user_id'] = $user_last[0]['user_id']+1;
				$data['username'] = isset($_REQUEST['username'])?$_REQUEST['username']:'';
				$data['password'] = isset($_REQUEST['password'])?MD5($_REQUEST['password']):'';
				$data['mobile'] = isset($_REQUEST['mobile'])?$_REQUEST['mobile']:'';
				$data['email'] = isset($_REQUEST['email'])?$_REQUEST['email']:'';
				if(empty($data['username'])){
		    		$data['error'] = $this->Model_m->data_error('4007'); 
			    	echo json_encode($data,JSON_UNESCAPED_UNICODE);
			    	exit;
				}	

				if(empty($data['password'])){
		    		$data['error'] = $this->Model_m->data_error('4007'); 
			    	echo json_encode($data,JSON_UNESCAPED_UNICODE);
			    	exit;
				}

				if(empty($data['mobile'])){
		    		$data['error'] = $this->Model_m->data_error('4007');
			    	echo json_encode($data,JSON_UNESCAPED_UNICODE);
			    	exit;
				}
				//添加用户
				$new_user = $this->Model_m->insert('users',$data);

				$user_data_where = array('user_id'=>$data['user_id']); //where
		        $user_weixin_where = array('user_id'=>$data['user_id']); //where
		        $user_data['success'] = $this->Model_m->data_success('4000');
		    	$user_data['user'] = $this->Model_m->select('users',$user_data_where);
		    	$user_data['weixin'] = $this->Model_m->select('users_weixin',$user_weixin_where);
		    		
		    	if($user_data['user'][0]['user_id']){
		    		echo json_encode($user_data,JSON_UNESCAPED_UNICODE);
		    	}
				exit;
			}
		}

		
		//修改用户**
		if(IS_PUT){
			//如果是PUT提交
			$user_id = isset($_REQUEST['user_id'])?$_REQUEST['user_id']:'';
			$user_where = array('user_id'=>$user_id); //where
			$user = $this->Model_m->select('users',$user_where);

			if(empty($user)){
				$data['error'] = $this->Model_m->data_error('4006');
		    	echo json_encode($data,JSON_UNESCAPED_UNICODE);
		    	exit;
			}else{

				$data['user_id'] = isset($_REQUEST['user_id'])?$_REQUEST['user_id']:'';
				$data['username'] = isset($_REQUEST['username'])?$_REQUEST['username']:'';
				$data['password'] = isset($_REQUEST['password'])?MD5($_REQUEST['password']):'';
				$data['mobile'] = isset($_REQUEST['mobile'])?$_REQUEST['mobile']:'';
				$data['email'] = isset($_REQUEST['email'])?$_REQUEST['email']:'';
					
				//修改用户

				$user_update = $this->Model_m->update('users',$user_where,$data);
				
				$user_data_where = array('user_id'=>$data['user_id']); //where
		        $user_weixin_where = array('user_id'=>$data['user_id']); //where
		        $user_data['success'] = $this->Model_m->data_success('4000');
		    	$user_data['user'] = $this->Model_m->select('users',$user_data_where);
		    	$user_data['weixin'] = $this->Model_m->select('users_weixin',$user_weixin_where);
		    			
		    	if($user_data['user'][0]['user_id']){
		    		echo json_encode($user_data,JSON_UNESCAPED_UNICODE);
		    	}
			}
			exit;
		}

		//删除用户**
		if(IS_DEL){
			//如果是IS_DEL提交
			$user_id = isset($_REQUEST['user_id'])?$_REQUEST['user_id']:'';
			$user_where = array('user_id'=>$user_id); //where
			$user = $this->Model_m->select('users',$user_where);

			if(empty($user)){
				$data['error'] = $this->Model_m->data_error('4006');
		    	echo json_encode($data,JSON_UNESCAPED_UNICODE);
		    	exit;
			}else{
				//修改用户
				$user_delete = $this->Model_m->delete('users',$user_where);
		        $user_data['success'] = $this->Model_m->data_success('4000');
		    	echo json_encode($user_data,JSON_UNESCAPED_UNICODE);
		    	
			}
			exit;
		}


		}

    	
        
    }
    

